package com.mycompany.app.hr.common.pagination;

import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * 封装分页和排序参数及分页查询结果.
 * 
 * @param <T> Page中的记录类型
 */
public class Page<T> {

    private int pageNo = 1;

    private int pageSize = -1;

    private String orderBy = null;

    private boolean asc = true;

    private boolean autoCount = false;

    private List<T> result = null;

    private int totalCount = -1;

    public Page() {

        // TODO Auto-generated constructor stub
    }

    public Page(int pageSize, boolean autoCount) {

        this.autoCount = autoCount;
        this.pageSize = pageSize;
    }

    /**
     * 每页的记录数量，无默认值.
     */
    public int getPageSize() {

        return pageSize;
    }

    public boolean isPageSizeSetted() {

        return pageSize > -1;
    }

    public void setPageSize(int pageSize) {

        this.pageSize = pageSize;
    }

    /**
     * 当前页的页号,序号从1开始.
     */
    public int getPageNo() {

        return pageNo;
    }

    public void setPageNo(int pageNo) {

        this.pageNo = pageNo;
    }

    /**
     * 第一条记录在结果集中的位置,序号从0开始.
     */
    public int getFirst() {

        if (pageNo < 1 || pageSize < 1)
            return -1;
        else
            return ((pageNo - 1) * pageSize);
    }

    public boolean isFirstSetted() {

        return (pageNo > 0 && pageSize > 0);
    }

    /**
     * 单个的排序字段.
     */
    public String getOrderBy() {

        return orderBy;
    }

    public void setOrderBy(String orderBy) {

        this.orderBy = orderBy;
    }

    public boolean isOrderBySetted() {

        return this.orderBy != null;
    }

    /**
     * 是否升序，默认为true.
     */
    public boolean isAsc() {

        return asc;
    }

    public void setAsc(boolean asc) {

        this.asc = asc;
    }

    /**
     * 是否自动获取总页数,默认为false,query by HQL时本属性无效.
     */
    public boolean isAutoCount() {

        return autoCount;
    }

    public void setAutoCount(boolean autoCount) {

        this.autoCount = autoCount;
    }

    /**
     * 页内的数据列表.
     */
    public List<T> getResult() {

        return result;
    }

    public void setResult(List<T> result) {

        this.result = result;
    }

    /**
     * 总记录数.
     */
    public int getTotalCount() {

        return totalCount;
    }

    public void setTotalCount(int totalCount) {

        this.totalCount = totalCount;
    }

    /**
     * 计算总页数.
     */
    public int getTotalPages() {

        if (totalCount == -1)
            return -1;

        int count = totalCount / pageSize;
        if (totalCount % pageSize > 0) {
            count++;
        }
        return count;
    }

    /**
     * 是否还有下一页.
     */
    public boolean isHasNext() {

        return (pageNo + 1 <= getTotalPages());
    }

    /**
     * 返回下页的页号,序号从1开始.
     */
    public int getNextPage() {

        if (isHasNext())
            return pageNo + 1;
        else
            return pageNo;
    }

    /**
     * 是否还有上一页.
     */
    public boolean isHasPre() {

        return (pageNo - 1 >= 1);
    }

    /**
     * 返回上页的页号,序号从1开始.
     */
    public int getPrePage() {

        if (isHasPre())
            return pageNo - 1;
        else
            return pageNo;
    }

    // ===============================================
    @Override
    public String toString() {

        return ToStringBuilder.reflectionToString(this,
                ToStringStyle.SHORT_PREFIX_STYLE);
    }
    // ===============================================
}
